package com.pxmeta.comment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pxmeta.comment.entity.Comment;
import com.pxmeta.comment.entity.dto.CommentDTO;
import com.pxmeta.comment.entity.vo.CommentVO;
import com.pxmeta.comment.mapper.CommentMapper;
import com.pxmeta.comment.service.ICommentService;
import com.pxmeta.comment.service.ICommentUserLikeService;
import com.pxmeta.common.constant.HttpStatus;
import com.pxmeta.common.exception.ServiceException;
import com.pxmeta.dynamic.entity.Dynamic;
import com.pxmeta.dynamic.entity.enums.MailInteractiveTypeEnum;
import com.pxmeta.dynamic.mapper.DynamicMapper;
import com.pxmeta.dynamic.service.IMailService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wcl
 * @since 2022-04-27
 */
@Service
@RequiredArgsConstructor
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {


    private final IMailService mailService;
    private final ICommentUserLikeService userLikeService;
    private final DynamicMapper dynamicMapper;

    @Override
    public List<CommentVO> getByDynamicId(Long currentUserId, Long dynamicId,Integer page, Integer size, Integer type, Long parentId) {
        List<CommentVO> list = baseMapper.getComments(new Page<>(page, size),dynamicId, type, parentId);
        for (CommentVO vo : list) {
            boolean like = userLikeService.isLike(currentUserId, vo.getId());
            vo.setIsLike(like);
        }
        return list;

    }


    @Override
    public Long comment(CommentDTO dto, Long userId) {

        Comment comment = new Comment();
        String content = dto.getContent();

        if(StringUtils.isBlank(content)){
            throw new ServiceException("评论不能为空！",HttpStatus.PARAMS_ERROR);
        }
        Long parentId = dto.getParentId();
        Long dynamicId = dto.getDynamicId();
        Long replyUserId = dto.getReplyUserId();

        Dynamic dynamic = dynamicMapper.selectById(dynamicId);

        if (null == dynamic){
            throw new ServiceException("动态不存在！", HttpStatus.DATA_NOT_FOUND_ERROR);
        }
        Long commentCount = dynamic.getCommentCount();
        commentCount++;
        dynamic.setCommentCount(commentCount);

        dynamicMapper.updateById(dynamic);
        comment.setUserId(userId);
        comment.setContent(content);
        comment.setParentId(parentId);
        comment.setDynamicId(dynamicId);
        comment.setLikeCount(0);
        comment.setReplyUserId(replyUserId);
        baseMapper.insert(comment);
        if (userId.equals(dynamic.getUserId())){
            return comment.getId();
        }
        if (replyUserId != null && replyUserId != 0){
            //回复
            mailService.sendInteractMessage(MailInteractiveTypeEnum.REPLY.getType(),userId, replyUserId,content,dynamic );
        }else {
            mailService.sendInteractMessage(MailInteractiveTypeEnum.COMMENT.getType(),userId, dynamic.getUserId(),content,dynamic);
        }
        return comment.getId();
    }



    @Override
    public Long getCommentCount(Long dynamicId) {
        LambdaQueryWrapper<Comment> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Comment::getDynamicId, dynamicId);
        return this.count(wrapper);
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void removeComment(Long commentId) {
        Comment comment = this.getById(commentId);
        if (null == comment){
            throw new ServiceException("该评论已被删除！");
        }
        Long dynamicId = comment.getDynamicId();
        Dynamic dynamic = dynamicMapper.selectById(dynamicId);
        Long commentCount = dynamic.getCommentCount();
        commentCount--;
        dynamic.setCommentCount(commentCount);
        dynamicMapper.updateById(dynamic);

        this.removeById(commentId);
    }
}
